package com.ch999.detect.View.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.widget.TextView;

import com.ch999.View.MDToolbar;
import com.ch999.detect.AppBaseActivity;
import com.ch999.detect.R;
import com.scorpio.mylib.Routers.MDRouters;
import com.scorpio.mylib.ottoBusProvider.BusEvent;
import com.scorpio.mylib.ottoBusProvider.BusProvider;

/**
 * Created by zhulang on 2018/1/9.
 */

public class DetectWebView extends AppBaseActivity {

    public static final String URL = "url";
    private MDToolbar mdToolbar;

    public Context mContext;
    private boolean isApp;
    private String mApikey;

    @Override
    public int getLayoutId() {
        return R.layout.layout_web_viwe;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mdToolbar = (MDToolbar) this.findViewById(R.id.toolbar);
        mContext =this;
        mdToolbar.setBackTitle("");
        mdToolbar.setRightTitle("");
        mdToolbar.setBackIcon(R.color.transparent);
        mdToolbar.setMainTitle("本机估价");
        mdToolbar.setOnMenuClickListener(new MDToolbar.OnMenuClickListener() {
            @Override
            public void onBackClick() {
                finish();
            }

            @Override
            public void onRigthClick() {

            }
        });

        WebView webView = (WebView) findViewById(R.id.webview);


        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setJavaScriptEnabled(true);

        CookieSyncManager.createInstance(this);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.removeAllCookie();


        CookieSyncManager.getInstance().sync();

        String stringExtra = getIntent().getStringExtra(URL);
        isApp = getIntent().getBooleanExtra("isApp", false);
        mApikey = getIntent().getStringExtra("apikey");
        cookieManager.setCookie(stringExtra,"isApp="+1);
        cookieManager.setCookie(stringExtra,"isHidden="+1);
        cookieManager.setCookie(stringExtra,"iszy="+mApikey);
        webView.loadUrl(stringExtra);

        TextView tvBottomAgain = (TextView) findViewById(R.id.tv_bottom_again);

        tvBottomAgain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isApp) {
                   // bundle.putString("isApp", "false");//集成主站使用true，revaluation
                  //  new MDRouters.Builder().build("jiuji://revaluation").bind(bundle).create(mContext).go();
                }
                Bundle bundle = new Bundle();
                bundle.putString("isApp", "false");//集成主站使用true，revaluation
                new MDRouters.Builder().build("jiuji://revaluation").bind(bundle).create(mContext).go();
//                startAnimActivity(MainActivityApp.class);
                BusEvent event = new BusEvent();

                event.setAction(NewDetectActivity.BUSACTION_CLOSE_ACTIVITY);
                BusProvider.getInstance().post(event);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                finish();
            }
        });

        findViewById(R.id.tv_change_arg).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //检测完成


                finish();
            }
        });


    }

    @Override
    public void findView() {

    }
}
